<template>
  <div>
    <div
      class="productContent line-bottom"
      v-for="item in list"
      @click="goto(item.CommodityCode)"
      v-if="list"
    >
      <div class="img">
        <img :src="item.SmallPic" />
      </div>
      <div class="inforPro">
        <p class="title">{{item.CommodityName}}</p>
        <p class="txt">{{item.SubTitle}}</p>
        <p class="price">
          <span class="priceRed">¥{{item.OriginalPrice}}</span>
          <span class="standard">{{item.Spec}}</span>
          <span class="addCart" @click.stop="addcard(item.CommodityId)"></span>
        </p>
      </div>
    </div>
    <div v-if="isok" class="notOrder">
      <div class="noOrderImg">
        <img src="//img07.yiguoimg.com/d/web/180315/013111/212501/noting@3x.png" />
        <p class="hint">没有搜索到“{{this.$route.query.value}}”相关商品，换个搜索关键词试试吧</p>
      </div>
    </div>
    <div class="option">
      <router-link class="num" to="/cart" tag="a">
        <i>{{this.$store.state.cartnum}}</i>
      </router-link>
      <vueToTop type="2" right="18" size="34" bottom="40" top="200"></vueToTop>
    </div>

    <like v-if="isok" />
  </div>
</template>
<script>
import like from "@/components/youlike.vue";
import getapi from "@/api/ajax";
export default {
  props: ["sort"],
  data() {
    return {
      pag: 1, //页数
      num: 4, //数据数量
      list: "", //渲染数据
      isok: false //无结果开关
    };
  },
  methods: {
    //添加购物车
    addcard(i) {
      let data = {
        id: i,
        num: 1
      };
      this.$store.dispatch("addcard", data);
    },
    //ajax请求
    async getdata() {
      try {
        let body = { CategoryCode: "", PageIndex: 1 };
        body.Sort = this.sort;
        //分类id
        if (this.$route.query.url) {
          body.CategoryId = this.$route.query.url;
        }
        //搜索id
        if (this.$route.query.value) {
          body.Keyword = this.$route.query.value;
        }
        let n = await getapi.getdata(
          "commodityapi/Commodity/GetSearchList",
          body
        );
        this.list = n.data.Data.CommodityList;
        //未找到相关商品开关
        if (this.list.length == 0) {
          this.isok = true;
        } else {
          this.isok = false;
        }
      } catch (err) {
        console.log(err);
      }
    },
    //路由跳转
    goto(id) {
      this.$router.push({ path: "/product/productlist/item", query: { id } });
    }
  },
  created() {
    this.getdata();
    this.$store.dispatch("cartnum");
  },

  watch: {
    sort: function() {
      this.getdata();
    }
  },
  components: {
    like
  }
};
</script>
<style lang="scss">
//未找到商品
.notOrder {
  margin-top: 6rem;
  .noOrderImg {
    margin: 4rem auto;
    img {
      width: 7.2rem;
      height: 7.2rem;
      display: block;
      margin: 0 auto;
    }
    .hint {
      font-size: 0.92rem;
      color: #808080;
      text-align: center;
      padding: 2rem 2.5rem 3.8rem;
      line-height: 1.2rem;
    }
  }
}
//每一件商品
.van-tab__pane {
  margin-top: 2.565625rem;
  .productContent {
    position: relative;
    padding: 0.943562rem 0.603875rem;
    box-sizing: border-box;
    overflow: hidden;
    background: #fff;
    height: 8.434375rem;
    .img {
      //图片
      position: relative;
      float: left;
      width: 6.548125rem;
      height: 6.548125rem;
      img {
        width: 6.548125rem;
        height: 6.548125rem;
      }
    }
    .inforPro {
      //文字部分
      float: right;
      width: 15.095625rem;
      text-align: left;
      .title {
        //大标题
        font-size: 0.86805rem;
        color: #333;
        overflow: hidden;
        text-overflow: ellipsis;
        display: -webkit-box;
        -webkit-line-clamp: 2;
        -webkit-box-orient: vertical;
        line-height: 1.125rem;
      }
      .txt {
        //小标题
        font-size: 0.75rem;
        color: #808080;
        padding: 0.566125rem 0 0.566125rem;
        white-space: nowrap;
        text-overflow: ellipsis;
        overflow: hidden;
      }
      .price {
        //价格
        position: relative;
        height: 2.074375rem;
        padding-top: 0.566125rem;
        z-index: 10;
        .addCart {
          z-index: 100;
          position: absolute;
          right: 1rem;
          top: 0.6rem;
          width: 1.6rem;
          height: 1.6rem;
          background: url(//img05.yiguoimg.com/d/web/180508/01311/150610/add.png)
            no-repeat;
          background-size: 100% 100%;
        }
      }
      .priceRed {
        //数字
        font-size: 0.868rem;
        color: #fd3d3d;
        padding-right: 0.849187rem;
        line-height: 1rem;
      }
      .standard {
        //规格
        font-size: 0.75rem;
        color: #808080;
        line-height: 0.875rem;
        padding-right: 0.377437rem;
      }
    }
  }
}
//购物车小图标
.option {
  position: fixed;
  bottom: 2rem;
  right: 1rem;
  z-index: 100;
  height: 5.9rem;
  .num {
    z-index: 100;
    position: relative;
    display: block;
    width: 2.2rem;
    height: 2.2rem;
    background: url(//img05.yiguoimg.com/d/web/180508/01311/150610/cart1.png)
      no-repeat;
    background-size: 100%;
    i {
      position: absolute;
      right: -0.6rem;
      top: -0.6rem;
      min-width: 0.6rem;
      height: 1rem;
      padding: 0 0.2rem;
      border: 1px solid #fff;
      border-radius: 0.5rem;
      background: #ff6353;
      line-height: 1rem;
      font-size: 0.6rem;
      font-style: normal;
      color: #fff;
      text-align: center;
    }
  }
}
</style>